const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        canvasHidden: true, //设置画板的显示与隐藏，画板不隐藏会影响页面正常显示
        shareImgPath: '',
        screenWidth: '', //设备屏幕宽度
        FilePath: '', //头像路径
        scope: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this

        that.setData(options)
        console.log(options)
        if (that.data.bg == 'null') {
            that.setData({
                bg: 'https://img.zhxcxmall.com/static/no-image-1.png'
            })
        }
        if(that.data.wx=='null'){
            that.setData({
                wx:'暂无'
            })
        }
        if (that.data.addr == 'null') {
            that.setData({
                addr: '暂无'
            })
        }
        console.log(that.data.bg)
        //获取用户设备信息，屏幕宽度
        wx.getSystemInfo({
            success: res => {
                that.setData({
                    screenWidth: res.screenWidth
                })
                console.log(that.data.screenWidth)
            }
        })

        that.saveImageToPhotosAlbum()


    },
    //定义的保存图片方法
    saveImageToPhotosAlbum: function() {
        wx.showLoading({
            title: '生成海报中...'
        })
        var that = this;
        //设置画板显示，才能开始绘图
        that.setData({
            canvasHidden: false
        })

        function download(url) {
            return new Promise(resolve => {
                wx.downloadFile({
                    url: url,
                    success: res => {
                        resolve(res.tempFilePath);
                    }
                })
            })
        }
        var unit = that.data.screenWidth / 375
        
        var context = wx.createCanvasContext('share')
        context.setFillStyle('#fff')
        context.fillRect(0, 0, unit*300, unit*600)

        // 大图
        download(that.data.bg).then(r => {
            context.drawImage(r, 0, 0, unit * 300, unit * 300);
            //logo
            return download(wx.getStorageSync('store_logo'))
            // return download('https://pimg.ec51.com//xcx/touxiang4.png')
        }).then(r => {
            var name = that.data.name
            context.setFontSize(15)
            context.setFillStyle("#333")
            context.fillText(name, unit * 15, unit * 328)
            context.drawImage(r, unit * 245, unit * 313, unit * 40, unit * 40)
        }).then(r => {
            // 职位
            var work = that.data.job
            context.setFontSize(12)
            context.setFillStyle("#999")
            context.fillText(work, unit * 15, unit * 348);
            return download('https://pimg.ec51.com//xcx/p_phone.png')
        }).then(r => {
            context.drawImage(r, unit * 15, unit * 385, unit * 10, unit * 10);
            var phone = that.data.phone
            context.setFillStyle('#555')
            context.setFontSize(11)
            context.fillText(phone, unit * 30, unit * 395)
            return download('https://pimg.ec51.com//xcx/p_wx.png')
        }).then(r => {
            context.setStrokeStyle('#e6e6e6')
            context.strokeRect(15, 370, 270, 0.1)
            context.drawImage(r, unit * 15, unit * 408, unit * 10, unit * 10)
            var wx_number = that.data.wx
            context.setFillStyle('#555')
            context.setFontSize(11)
            context.fillText(wx_number, unit * 30, unit * 415)
            return download('https://pimg.ec51.com//xcx/p_addr.png')
        }).then(r => {
            context.drawImage(r, unit * 15, unit * 427, unit * 10, unit * 10)
            var addr = that.data.addr
            context.setFillStyle('#555')
            context.setFontSize(11)
            context.fillText(addr, unit * 30, unit * 435);
            return download('https://xcx.ec51.com/v1/login/show-qyim-card-qr?id=' + that.data.id + '&company_id=' + wx.getStorageSync('company_id'));
        }).then(r => {
            console.log(r);
            context.drawImage(r, unit * 220, unit * 385, unit * 65, unit * 65)
        }).then(r => {
            context.draw(false, function() {
                wx.hideLoading();
                // return;
            })
        })
    },
    button() {
        var that = this
        var unit = that.data.screenWidth / 375
        wx.showLoading({
            title: '保存中',
        })
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: unit * 375,
            height: unit * 600,
            destWidth: unit * 720,
            destHeight: unit * 1100,
            quality: 1,
            canvasId: 'share',
            success: function(res) {
                that.setData({
                    shareImgPath: res.tempFilePath,
                    scope: false
                })
                if (!res.tempFilePath) {
                    wx.showModal({
                        title: '提示',
                        content: '海报生成中，请稍后重试',
                        showCancel: false
                    })
                }

                // wx.getSetting({
                //     success:res=>{
                //         if (res.authSetting['scope.writePhotosAlbum']) {
                //             that.setData({
                //                 scope: false
                //             })
                //         } else {
                //             that.setData({
                //                 scope: true
                //             })
                //         }
                //     }
                // })
                //画板路径保存成功后，调用方法吧图片保存到用户相册
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    //保存成功失败之后，都要隐藏画板，否则影响界面显示。
                    success: (res) => {
                        setTimeout(() => {
                            wx.showModal({
                                title: '保存海报成功',
                                content: '名片海报已保存到手机相册!',
                                showCancel: false
                            })
                        }, 500)
                        wx.hideLoading()
                        that.setData({
                            canvasHidden: true
                        })
                    },
                    fail: (err) => {
                        console.log(err, '123456789');
                        wx.hideLoading()
                        wx.showToast({
                            title: '保存失败，请稍后再试',
                            icon: 'none'
                        })
                        if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                            console.log("当初用户拒绝，再次发起授权")
                            that.setData({
                                scope: true
                            })
                        }
                    }
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})